package baowan

import (
	"encoding/json"
	"fmt"
	"github.com/valyala/fasthttp"
	"kop/modules/pay/services"
	"strconv"
	"strings"
)

func Handler(ctx *fasthttp.RequestCtx) error {

	var paths = strings.Split(string(ctx.Path()), "/")
	if len(paths) < 4 {
		return fmt.Errorf("path error")
	}

	var orderID, err = strconv.Atoi(paths[3])
	if err != nil {
		return err
	}

	return services.Handler.CallBack(orderID)

}

type Res struct {
	Code   int    `json:"resultCode"`
	ErrMsg string `json:"errMsg"`
}

func (r *Res) Ok() string {
	r.Code = 1
	b, _ := json.Marshal(r)
	return string(b)
}

func (r *Res) Err(s string) string {
	r.Code = -1
	r.ErrMsg = s
	b, _ := json.Marshal(r)
	return string(b)
}
